    //
//  MCTabBarNavigationController.m
//  MClient
//
//  Created by apple_cyg on 10-11-14.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MCTabBarNavigationController.h"
#import "MCErrorManagement.h"
#import "MCUserDefaults.h"
#import "MCFunction.h"
#import "MServerDAO.h"
#import "MenuController.h"

@implementation MCTabBarNavigationController
@synthesize menuData;
@synthesize menuTitle;
@synthesize isAppMenu;
@synthesize needBackBtn;


- (void)viewDidLoad {
	[super viewDidLoad];
	
	self.delegate = self;
	self.isAppMenu = NO;
	self.title = self.tabBarItem.title;
}


- (void)dealloc {
	[menuTitle release];
	[super dealloc];
}

- (void)onRequestMenu  {
	@try {
		if ([self.viewControllers count] > 0) {
			((UIViewController*)[self.viewControllers objectAtIndex:0]).view.alpha = 0.0;
		}
		
		NSMutableDictionary *url = [self.menuData objectForKey:@"url"];
		
		NSMutableDictionary *otherParams = [[[NSMutableDictionary alloc] init] autorelease];
		[otherParams setObject:[self.menuData objectForKey:@"id"]==nil?@"":[self.menuData objectForKey:@"id"] forKey:@"authValue"];
		[otherParams setObject:@"menu" forKey:@"authKey"];
		
		NSString *actionType = @"empty_url";
		if (url != nil && [url objectForKey:@"actionType"]) {
			actionType = [url objectForKey:@"actionType"];
		}
		
		if ([actionType isEqualToString:@"empty_url"]) {
			menuTitle = [self.menuData objectForKey:@"label"];
			if (isAppMenu) {
				[[MServerDAO sharedInstance] getMenuOfAppAsync:[self.menuData objectForKey:@"id"]
												   otherParams:otherParams
													  delegate:self];
			} else {
				[[MServerDAO sharedInstance] getSubMenuAsync:[self.menuData objectForKey:@"id"]
												 otherParams:otherParams
													delegate:self];
			}
		}
		else {		
			[MCFunction pushViewByUrl:url parameters:otherParams confirmMessage:nil naviController:/*(MCTabBarNavigationController *)*/self];
		}
	}
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"100" returnMessage:[e reason]];
	}
}

- (void)getDataFromServerSuccess: (NSMutableDictionary *)returnValue {
	
	MenuController *menuViewController = [[MenuController alloc] init];
	menuViewController.title = self.menuTitle;
	menuViewController.menus = [MenuController removeSeqMenu:(NSMutableArray *)returnValue];
	
	[self pushViewController:menuViewController animated:YES];
	[menuViewController release];
}

// 从服务器取数据失败
- (void)getServerError: (NSString *)returnCode returnMessage:(NSString *)returnMessage
{
	[[MCErrorManagement instance] doError:returnCode returnMessage:returnMessage];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
	MCLog(@"MCTabBarNavigationController push %@", [viewController description]);
	if ([self.viewControllers count] == 1 && !needBackBtn) {
		viewController.navigationItem.leftBarButtonItem = nil;
		viewController.navigationItem.hidesBackButton = YES;
		if ([viewController isKindOfClass:[MenuController class]]) {
			viewController.navigationItem.rightBarButtonItem = nil;
		}
	}
	[super pushViewController:viewController animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
	if ([self.viewControllers count] == 2) {
		UIViewController* viewController = [self.viewControllers objectAtIndex:0];
		viewController.view.alpha = 1.0;
	}
	if (!needBackBtn) {
		return [super popViewControllerAnimated:animated];
	} else {
		if ([self.viewControllers count] == 2 &&
			[[self.viewControllers objectAtIndex:0] isKindOfClass:[UIViewController class]] &&
			![[self.viewControllers objectAtIndex:0] isKindOfClass:[MCViewController class]]) {
			self.navigationController.navigationBarHidden = NO;
			self.navigationBar.hidden = YES;
            //[self performSelector:@selector(onRequestMenu) withObject:nil afterDelay:.5];
            //return [super popViewControllerAnimated:animated];
			return [self.navigationController popViewControllerAnimated:animated];
		} else {          
            
			return [super popViewControllerAnimated:animated];
		}
	}
}

@end
